package com.tencent.qf.team.service.impl;

import com.tencent.qf.team.dao.TyOrderDao;
import com.tencent.qf.team.dto.db.UserOrderCpsStatisticDo;
import com.tencent.qf.team.dto.request.UserOrderCpsStatisticRequest;
import com.tencent.qf.team.dto.response.UserOrderCpsStatisticResponse;
import com.tencent.qf.team.service.UserOrderCpsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserOrderCpsServiceImpl implements UserOrderCpsService {

    @Autowired
    private TyOrderDao tyOrderDao;

    /**
     * 用户个人(不包含下属)维度累计聚合
     *
     * @param request
     * @return
     */
    @Override
    public List<UserOrderCpsStatisticResponse> userOrderCpsStatistic(UserOrderCpsStatisticRequest request) {
        UserOrderCpsStatisticDo userOrderCpsStatisticDo = new UserOrderCpsStatisticDo();
        BeanUtils.copyProperties(request, userOrderCpsStatisticDo);
        if (StringUtils.isNotEmpty(request.getStartDate())) {
            long epochMilli = LocalDateTime.parse(request.getStartDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toInstant(ZoneOffset.of("+8")).toEpochMilli();
            userOrderCpsStatisticDo.setStartTime(epochMilli);
        }
        if (StringUtils.isNotEmpty(request.getEndDate())) {
            long epochMilli = LocalDateTime.parse(request.getEndDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toInstant(ZoneOffset.of("+8")).toEpochMilli();
            userOrderCpsStatisticDo.setEndTime(epochMilli);
        }

        Map<String, UserOrderCpsStatisticResponse> resultMap = new HashMap<>();
        for (String uid : request.getUids()) {
            resultMap.put(uid, new UserOrderCpsStatisticResponse(uid));
        }

        List<UserOrderCpsStatisticResponse> resultList = tyOrderDao.userOrderCpsStatistic(userOrderCpsStatisticDo);

        if (!CollectionUtils.isEmpty(resultList)) {
            for (UserOrderCpsStatisticResponse item : resultList) {
                if (resultMap.containsKey(item.getUid())) {
                    UserOrderCpsStatisticResponse response = resultMap.get(item.getUid());
                    BeanUtils.copyProperties(item, response);
                }
            }
        }
        if ("1".equals(request.getReturnTotal())) {
            // 求累计，不带日期条件重新查询一次
            userOrderCpsStatisticDo.setStartTime(null);
            userOrderCpsStatisticDo.setEndTime(null);
            List<UserOrderCpsStatisticResponse> sumList = tyOrderDao.userOrderCpsStatistic(userOrderCpsStatisticDo);
            if (!CollectionUtils.isEmpty(resultList)) {
                for (UserOrderCpsStatisticResponse item : sumList) {
                    if (resultMap.containsKey(item.getUid())) {
                        UserOrderCpsStatisticResponse response = resultMap.get(item.getUid());
                        response.setBuyerOrderCommissionAmountTotal(item.getBuyerOrderCommissionAmount());
                        response.setBuyerOrderCommissionCountTotal(item.getBuyerOrderCommissionCount());
                    }
                }
            }
        }
        return resultList;
    }
}
